package com.cpkso.buss.backend.user.domain;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Index;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.runze.base.domain.RecordEntity;

/**
 * 权限：
 * 
 * TODO 需要权限组吗？
 * @author wrzhxy@qq.com
 * @date 2018年3月13日
 */
@Entity
@Table(name="permission", indexes=
	{@Index(name="idx_permission_name", columnList="name")})
@Cache(usage= CacheConcurrencyStrategy.READ_WRITE)
public class Permission extends RecordEntity {
	// 权限名称
	@Basic(optional=false)
	@Column(unique=true)
	private String name;
	// 描述
	private String description;
	
	@ManyToMany(fetch=FetchType.LAZY, mappedBy="permissions")
//	@JoinTable(name="role_permission", 
//		joinColumns=@JoinColumn(name="rid"),
//		inverseJoinColumns=@JoinColumn(name="pid"))
	@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
	private Set<Role> roles = new HashSet<>();
	
	public Permission() {
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public Set<Role> getRoles() {
		return roles;
	}
	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((super.getId() == null) ? 0 : super.getId().hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Permission other = (Permission) obj;
		if (super.getId() == null) {
			if (other.getId() != null)
				return false;
		} else if (!super.getId().equals(other.getId()))
			return false;
		return true;
	}
	
}
